import wx
import wx.richtext as rt


        
class RichTextCtrl(rt.RichTextCtrl):
    def __init__(self,*args,**kw):
        rt.RichTextCtrl.__init__(self,*args,**kw)
    

class RichTextFrame(wx.Frame):
    def __init__(self,*args,**kw):
        wx.Frame.__init__(self,*args,**kw)
        
        # create status bar
        self.CreateStatusBar()
        self.SetStatusText('genWriter')        

        self.MakeMenuBar()
        
             
        self.rtc = rt.RichTextCtrl(self,style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER)
        wx.CallAfter(self.rtc.SetFocus)
        self.rtc.Freeze()
        self.rtc.BeginSuppressUndo()
        self.rtc.BeginParagraphSpacing(0,20)
        self.rtc.EndParagraphSpacing()
        self.rtc.EndSuppressUndo()
        self.rtc.Thaw()
        #self.Show(True)
        
    def MakeMenuBar(self):
        def doBind(item, handler, updateUI=None):
            self.Bind(wx.EVT_MENU, handler, item)
            if updateUI is not None:
                self.Bind(wx.EVT_UPDATE_UI, updateUI, item)

                        
        # set up filemenu
        filemenu = wx.Menu()
        doBind(filemenu.Append(-1, "&Save\tCtrl+S", "Save"),
                self.OnFileSave )   
        doBind(filemenu.Append(-1, "Save As", "Save As"),
                self.OnFileSaveAs )   
        filemenu.AppendSeparator()
        doBind(filemenu.Append(-1, "&About\tCtrl+A", "Information about this program"),
                self.OnAbout )        
        doBind(filemenu.Append(-1, "E&xit\tCtrl+Q", "Quit this program"),
                self.OnExit )         
        
        # set up editmenu
        editmenu = wx.Menu()
        doBind(editmenu.Append(-1, "&Bold\tCtrl+B"),
                self.OnBold )        
        
        # set up formatmenu
        formatmenu = wx.Menu()
        doBind(formatmenu.Append(-1,'Single spacing','Single spacing'),self.OnLineSpacingSingle)
        doBind(formatmenu.Append(-1,'Half spacing','Half spacing'),self.OnLineSpacingHalf)
        doBind(formatmenu.Append(-1,'Double spacing','Double spacing'),self.OnLineSpacingDouble)
        
        
        # create the menubar
        menubar = wx.MenuBar()
        menubar.Append(filemenu,'&File')
        menubar.Append(editmenu,'&Edit')        
        menubar.Append(formatmenu,'Format')        
        self.SetMenuBar(menubar)        
        
    # event handlers
    def OnLineSpacingSingle(self, evt): 
        attr = rt.TextAttrEx()
        attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING)
        ip = self.rtc.GetInsertionPoint()
        if self.rtc.GetStyle(ip, attr):
            r = rt.RichTextRange(ip, ip)
            if self.rtc.HasSelection():
                r = self.rtc.GetSelectionRange()

            attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING)
            attr.SetLineSpacing(10)
            self.rtc.SetStyle(r, attr)
 
                
    def OnLineSpacingHalf(self, evt):
        attr = rt.TextAttrEx()
        attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING)
        ip = self.rtc.GetInsertionPoint()
        if self.rtc.GetStyle(ip, attr):
            r = rt.RichTextRange(ip, ip)
            if self.rtc.HasSelection():
                r = self.rtc.GetSelectionRange()

            attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING)
            attr.SetLineSpacing(15)
            self.rtc.SetStyle(r, attr)

        
    def OnLineSpacingDouble(self, evt):
        attr = rt.TextAttrEx()
        attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING)
        ip = self.rtc.GetInsertionPoint()
        if self.rtc.GetStyle(ip, attr):
            r = rt.RichTextRange(ip, ip)
            if self.rtc.HasSelection():
                r = self.rtc.GetSelectionRange()

            attr.SetFlags(rt.TEXT_ATTR_LINE_SPACING)
            attr.SetLineSpacing(20)
            self.rtc.SetStyle(r, attr)
            
    def OnBold(self, eventt):
        self.rtc.ApplyBoldToSelection()

    
    def OnAbout(self,event):
        d = wx.MessageDialog(self,
                                'genWriter is written \n in wxPython','About Sample Editor', wx.OK)
        d.ShowModal() # shows it
        d.Destroy() # destroys it when finished

    def OnExit(self,event):
        self.Close(True) # close the frame

    def OnFileSave(self, evt):
        if not self.rtc.GetFilename():
            self.OnFileSaveAs(evt)
            return
        self.rtc.SaveFile()


    def OnFileSaveAs(self, evt):
        wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=True)

        dlg = wx.FileDialog(self, "Choose a filename",
                            wildcard=wildcard,
                            style=wx.SAVE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            if path:
                fileType = types[dlg.GetFilterIndex()]
                ext = rt.RichTextBuffer.FindHandlerByType(fileType).GetExtension()
                if not path.endswith(ext):
                    path += '.' + ext
                self.rtc.SaveFile(path, fileType)
        dlg.Destroy()
        
if __name__ == '__main__':        
    app = wx.PySimpleApp()
    frame = RichTextFrame(None,title="genWriter")
    app.MainLoop()